/*
 * Ext JS Library 3.0 RC2
 * Copyright(c) 2006-2009, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

Ext.onReady(function(){

    Ext.QuickTips.init();

    // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'side';

    var bd = Ext.getBody();

    /*
     * ================  Translation form  =======================
     */    
    Ext.translate.Manager.Init({URL : '../../TranslateServlet',
				        languageImagesURL : '../../img/flags/',
				        autocreateIonsCSS : true,
				        id : 'singleform',
				        autoload : true
				        ,cookieReset : false
				        ,isAlphaIndex : true
				        });
    
    bd.createChild({tag: 'h2', html: 'Translation selector'});

    var calldata = [
                              ['../../TranslateServlet', 'Java Call' ],
                              ['../../php/gtranslate-api/translate.php', 'PHP Call' ]
                 ];
    
 // simple array store
    var store = new Ext.data.ArrayStore({
    	id: 0,
        fields: ['path', 'desc'],
        data : calldata
    });
    
    var combo = new Ext.form.ComboBox({
    	forceSelection : true,
    	editable : false, 
    	allowBlank : false,
        store: store,
        displayField:'desc',
        valueField: 'path',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        emptyText:'Select Java or PHP call...',
        fieldLabel : 'Type of server call',
        selectOnFocus:true,
        //custom translation testing 
        customField :'Optional settings',
        translateItems : ['customField']        
    });
    
    combo.translateCallbackFn = function (options ,success ,response,field){
    	//alert(this.translateLoadOriginal(field));
    	//alert(response.responseText);
    	};
    
    combo.on('select', function() {	
	  
    	Ext.translate.URL = this.getValue();
    	Ext.getCmp(languageId).disable();	
	  		var fn = function (){
  				  
				  Ext.getCmp(languageId).store.loadData(Ext.translate.languages);
				  Ext.getCmp(languageId).enable();	 
  		};	
  
  		Ext.translate.Manager.getSupportedLanguages(fn.createDelegate(this));
 
    });
    
    var _formPanel = new Ext.form.FormPanel({
       region: 'center',
       border: false,
       bodyStyle: "padding: 5px;",
       baseCls: 'x-plain',
       labelWidth: 'auto',
       defaults: {
         width: 250
       },
       items: [combo,new Ext.translate.LanguageCombo()  ]

     });    
    
 
    _formPanel.render(document.body);
    
   
    /*
     * ================  Form 2  =======================
     */
    bd.createChild({tag: 'h2', html: 'Form 2 - Adding fieldsets'});

    var fsf = new Ext.FormPanel({
        labelWidth: 75, // label settings here cascade unless overridden
        url:'save-form.php',
        frame:true,
        title: 'Simple Form with FieldSets',
        bodyStyle:'padding:5px 5px 0',
        width: 350,

        items: [{
            xtype:'fieldset',
            checkboxToggle:true,
            title: 'User Information',
            autoHeight:true,
            defaults: {width: 210},
            defaultType: 'textfield',
            collapsed: true,
            items :[{
                    fieldLabel: 'First Name',
                    name: 'first',
                    allowBlank:false
                },{
                    fieldLabel: 'Last Name',
                    name: 'last'
                },{
                    fieldLabel: 'Company',
                    name: 'company'
                }, {
                    fieldLabel: 'Email',
                    name: 'email',
                    vtype:'email'
                }
            ]
        },{
            xtype:'fieldset',
            title: 'Phone Number',
            collapsible: true,
            autoHeight:true,
            defaults: {width: 210},
            defaultType: 'textfield',
            items :[{
                    fieldLabel: 'Home',
                    name: 'home',
                    value: '(888) 555-1212'
                },{
                    fieldLabel: 'Business',
                    name: 'business'
                },{
                    fieldLabel: 'Mobile',
                    name: 'mobile'
                },{
                    fieldLabel: 'Fax',
                    name: 'fax'
                }
            ]
        }],

        buttons: [{
            text: 'Save'
        },{
            text: 'Cancel'
        }]
    });

    fsf.render(document.body);

    Ext.translate.Manager.applyTranslation(Ext.translate.currentLanguage);
 
});